import com.alibaba.fastjson.TypeReference;
import entity.Cultivate;
import entity.Exam;
import entity.Materials;
import entity.Question;
import request.*;
import response.BaseResponse;
import util.HttpUtil;
import util.TrainConstant;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 刷题入口类
 */
public class TrainMain {

    public static void main(String[] args) {
        //1、先刷课，再考试
        //不加页面获取所有数据？
        ThemeListRequest request = new ThemeListRequest();
        request.setAssId(262);
        request.setDelFlag(0);
        TypeReference<BaseResponse<List<Cultivate>>> tr = new TypeReference<BaseResponse<List<Cultivate>>>() {
        };
        List<Cultivate> list = HttpUtil.get(TrainConstant.THEME_LIST_URL, request, tr);
        if(list == null || list.size() == 0){
            System.out.println("请求数据列表为空");
            return;
        }
        //筛选出所有不合格的专题
        List<Cultivate> unCompleteList = list.stream().filter(x->x.getPassStatus() != null && x.getPassStatus().equals("不合格")).collect(Collectors.toList());
        for(Cultivate cultivate : unCompleteList){
            CultivateListRequest cultivateListRequest = new CultivateListRequest();
            cultivateListRequest.setCultivateId(cultivate.getId());
            TypeReference<BaseResponse<List<Materials>>> materialsTr = new TypeReference<BaseResponse<List<Materials>>>(){};
            List<Materials> materialsList = HttpUtil.get(TrainConstant.COURSE_LIST_URL,cultivateListRequest,materialsTr);
            if(materialsList == null || materialsList.size() == 0){
                System.out.println("课程列表为空");
                return;
            }
            for(Materials materials : materialsList){
                if(materials.getPassStatus() != null && materials.getPassStatus().equals("合格")){
                    continue;
                }
                EditMaterialsReadCountRequest readCountRequest = new EditMaterialsReadCountRequest();
                readCountRequest.setCultivateId(cultivate.getId());
                readCountRequest.setMaterialsId(materials.getId());
                TypeReference<BaseResponse<Integer>> materialsDetailTr = new TypeReference<BaseResponse<Integer>>(){};
                Integer data = HttpUtil.get(TrainConstant.VIDEO_DETAIL_URL,readCountRequest,materialsDetailTr);
                if(data == null || data != 1){
                    System.out.println("刷课失败");
                    return;
                }
                Integer duration = Integer.parseInt(materials.getReadDuration());
                UpdateReadTimeRequest readTimeRequest = new UpdateReadTimeRequest();
                readTimeRequest.setCultivateId(cultivate.getId());
                readTimeRequest.setMaterialsId(materials.getId());
                readTimeRequest.setTimeSum(duration);
                readTimeRequest.setUserId("3602bf010a334002aba202129ff3ea0c");
                TypeReference<BaseResponse<Integer>> readTr = new TypeReference<BaseResponse<Integer>>(){};
                Integer result = HttpUtil.post(TrainConstant.UPDATE_READ_TIME_URL,readTimeRequest,readTr);
                System.out.println("刷题结果："+result);
            }
        }
        //所有课程都刷新一遍
        if(list != null && list.size() > 0){
            list.forEach(x->{
                TypeReference<BaseResponse<String>> typeReference = new TypeReference<BaseResponse<String>>(){};
                String url = TrainConstant.REFRESH_STATUS_URL + x.getId();
                HttpUtil.get(url, null,typeReference);
            });
        }
        //找到已刷课，未考试的课程
        List<Cultivate> examList = HttpUtil.get(TrainConstant.THEME_LIST_URL, request, tr);
        if(list == null || list.size() == 0){
            System.out.println("请求数据列表为空");
        }
        List<Map<String,String>> unCompletedList = new ArrayList<>();
        List<Cultivate> unExamList = examList.stream().filter(x->{
            if(x.getPassStatus() == null || x.getPassStatus().equals("不合格")){
                Map<String,String> map = new HashMap<>();
                map.put(x.getId(),x.getName());
                unCompletedList.add(map);
                return false;
            }
            if(null == x.getExamCount() || (null != x.getExamPassOrFail() && x.getExamPassOrFail().equals("及格"))){
                return false;
            }
            if(null == x.getExamPassCount() || x.getExamPassCount() == 0){
                return true;
            }
            if(x.getExamCount() > x.getExamPassCount()){
                return true;
            }
            return false;
        }).collect(Collectors.toList());
        for(Cultivate cultivate : unExamList){
            CultivateListRequest cultivateListRequest = new CultivateListRequest();
            cultivateListRequest.setCultivateId(cultivate.getId());
            TypeReference<BaseResponse<List<Materials>>> materialsTr = new TypeReference<BaseResponse<List<Materials>>>(){};
            List<Materials> materialsList = HttpUtil.get(TrainConstant.COURSE_LIST_URL,cultivateListRequest,materialsTr);
            if(materialsList == null || materialsList.size() == 0){
                System.out.println("课程列表为空");
                return;
            }
            for(Materials materials : materialsList){
                if(null == materials.getExamId() || (materials.getExamPassOrFail() != null && materials.getExamPassOrFail().equals("及格"))){
                    continue;
                }
                Map<String,Object> getNowExamParam = new HashMap<>();
                getNowExamParam.put("id",materials.getExamId());
                TypeReference<BaseResponse<String>> tr1 = new TypeReference<BaseResponse<String>>(){};
                HttpUtil.post(TrainConstant.GET_NOW_EXAM,getNowExamParam,tr1);
                Map<String,Object> getExamListParam = new HashMap<>();
                getExamListParam.put("userId","3602bf010a334002aba202129ff3ea0c");
                getExamListParam.put("delFlag","0");
                getExamListParam.put("isHistory","0");
                getExamListParam.put("reasonable",false);
                getExamListParam.put("deptId","");
                Map<String,String> nameMap = new HashMap<>();
                nameMap.put("name","");
                getExamListParam.put("manage",nameMap);
                getExamListParam.put("examId",materials.getExamId());
                TypeReference<BaseResponse<List<Exam>>> examTr = new TypeReference<BaseResponse<List<Exam>>>(){};
                List<Exam> exams = HttpUtil.post(TrainConstant.GET_EXAM_LIST,getExamListParam,examTr);
                if(exams == null || examList.size() <= 0){
                    continue;
                }
                for(Exam exam: exams){
                    Map<String,Object> createPaperParam = new HashMap<>();
                    createPaperParam.put("id",exam.getId());
                    TypeReference<BaseResponse<List<Question>>> queTr = new TypeReference<BaseResponse<List<Question>>>(){};
                    List<Question> questions = HttpUtil.post(TrainConstant.CREATE_PAPER,createPaperParam,queTr);
                    if(null == questions || questions.size() <= 0){
                        continue;
                    }
                    Map<String,Object> downloadPaperParam = new HashMap<>();
                    downloadPaperParam.put("id",exam.getId());
                    TypeReference<BaseResponse<String>> tr2 = new TypeReference<BaseResponse<String>>(){};
                    HttpUtil.post(TrainConstant.SEND_DOWNLOAD_SUCCESS,downloadPaperParam,tr2);

                    Map<String,Object> canExam = new HashMap<>();
                    canExam.put("id",exam.getId());
                    canExam.put("examId",exam.getExamId());
                    TypeReference<BaseResponse<String>> tr3 = new TypeReference<BaseResponse<String>>(){};
                    HttpUtil.post(TrainConstant.CAN_EXAM,canExam,tr3);

                    Map<String,Object> addParam = new HashMap<>();
                    addParam.put("examId",exam.getExamId());
                    TypeReference<BaseResponse<String>> tr4= new TypeReference<BaseResponse<String>>(){};
                    HttpUtil.post(TrainConstant.ADDITION_LIST,addParam,tr4);

                    Map<String,Object> startExam = new HashMap<>();
                    startExam.put("id",exam.getId());
                    LocalDateTime now = LocalDateTime.now();
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    String formattedDateTime = now.minusMinutes(3).format(formatter);
                    startExam.put("realStartTime",formattedDateTime);
                    TypeReference<BaseResponse<Object>> tr5= new TypeReference<BaseResponse<Object>>(){};
                    HttpUtil.post(TrainConstant.START_EXAM,startExam,tr5);

                    SubmitExam param = new SubmitExam();
                    param.setId(exam.getId().toString());
                    param.setExamId(exam.getExamId());
                    param.setObjectScore(100);
                    String endTime = now.format(formatter);
                    param.setRealEndTime(endTime);
                    List<QuestionList> questionList = new ArrayList<>();
                    for(Question question : questions){
                        QuestionList ql = new QuestionList();
                        ql.setId(question.getId());
                        ql.setStudentAnswer(question.getAnswer());
                        ql.setStudentScore(question.getScore());
                        questionList.add(ql);
                    }
                    param.setQuestionList(questionList);
                    TypeReference<BaseResponse<Object>> tr6 = new TypeReference<BaseResponse<Object>>(){};
                    HttpUtil.post(TrainConstant.SUBMIT_EXAM,param,tr6);

                    Map<String,Object> refreshParam = new HashMap<>();
                    refreshParam.put("paperId",exam.getId());
                    refreshParam.put("equipment","2");
                    TypeReference<BaseResponse<Object>> tr7= new TypeReference<BaseResponse<Object>>(){};
                    HttpUtil.post(TrainConstant.REFRESH_EXAM_STATUS,refreshParam,tr4);
                }
            }
        }

        if(unCompletedList != null && unCompletedList.size() > 0){
            System.out.println("存在未完成刷课的课程：" + unCompletedList.toString());
        }
    }
}
